package com.zzw.bridge.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.bridge.business.dao.ZzwPierColumnReportDao;
import com.zzw.bridge.business.dao.ZzwPierColumnReportDetailDao;
import com.zzw.bridge.business.dao.ZzwPierColumnReportHisDao;
import com.zzw.bridge.business.dto.NoPassReportDTO;
import com.zzw.bridge.business.dto.ReportQueryDTO;
import com.zzw.bridge.business.dto.ReportStatisticDTO;
import com.zzw.bridge.business.dto.ZzwPierColumnReportDTO;
import com.zzw.bridge.business.dto.ZzwPierProjectDTO;
import com.zzw.bridge.business.service.ZzwPierColumnReportService;
import com.zzw.bridge.business.service.ZzwPierProjectService;
import com.zzw.bridge.business.vo.ReportStatisticVo;
import com.zzw.bridge.business.vo.ZzwPierColumnReportDetailVo;
import com.zzw.bridge.business.vo.ZzwPierColumnReportHisVo;
import com.zzw.bridge.business.vo.ZzwPierColumnReportInfoVo;
import com.zzw.bridge.business.vo.ZzwPierColumnReportVo;
import com.zzw.bridge.business.vo.ZzwPierProjectVo;
import com.zzw.bridge.common.entitys.business.ZzwPierColumnReportEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class ZzwPierColumnReportServiceImpl extends ServiceImpl<ZzwPierColumnReportDao, ZzwPierColumnReportEntity> implements ZzwPierColumnReportService {

    @Resource
    private ZzwPierColumnReportDetailDao reportDetailDao;

    @Resource
    private ZzwPierColumnReportHisDao reportHisDao;

    @Resource
    private ZzwPierProjectService projectService;

    @Override
    public String editProject(ZzwPierColumnReportDTO dto) {
        Integer integer = baseMapper.updateByReportId(dto);
        return String.format("成功编辑%s条数据",integer);
    }

    @Override
    public ZzwPierColumnReportInfoVo queryReportInfo(String reportId) {
        ZzwPierColumnReportEntity entity = baseMapper.selectById(reportId);
        ZzwPierColumnReportInfoVo vo = new ZzwPierColumnReportInfoVo();
        BeanUtil.copyProperties(entity,vo);
        List<ZzwPierColumnReportDetailVo> list = reportDetailDao.selectListByReportId(reportId);
        vo.setSectionData(list);
        return vo;
    }

    @Override
    public ReportStatisticVo reportStatistics(ReportStatisticDTO dto, HttpServletRequest request) {
        ZzwPierProjectDTO projectDto = new ZzwPierProjectDTO();
        List<ZzwPierProjectVo> list = projectService.queryPage(projectDto, request);
        ZzwPierProjectServiceImpl impl = new ZzwPierProjectServiceImpl();
        ZzwPierProjectVo projectVo = impl.findVoById(list, dto.getId());
        List<String> projectCodeList = getZzwPierProjectVoList(projectVo);
        ReportStatisticVo vo = baseMapper.reportStatistics(dto,projectCodeList);
        if (ObjectUtil.isNotNull(vo)) {
            vo.setNoPassNum(vo.getTotal()- vo.getPassNum());
        }
        return vo;
    }

    @Override
    public Page<ZzwPierColumnReportHisVo> queryHisPage(ReportQueryDTO dto) {
        Page<ZzwPierColumnReportHisVo> page = new Page<>(dto.getPage(), dto.getSize());
        page=reportHisDao.selectHisByPage(page,dto);
        return page;
    }

    @Override
    public Page<ZzwPierColumnReportVo> queryNoPassReportPage(NoPassReportDTO dto, HttpServletRequest request) {
        ZzwPierProjectDTO projectDto = new ZzwPierProjectDTO();
        List<ZzwPierProjectVo> list = projectService.queryPage(projectDto, request);
        ZzwPierProjectServiceImpl impl = new ZzwPierProjectServiceImpl();
        ZzwPierProjectVo projectVo = impl.findVoById(list, dto.getId());
        List<String> projectCodeList = getZzwPierProjectVoList(projectVo);
        Page<ZzwPierColumnReportVo> page = new Page<>(dto.getPage(), dto.getSize());
        page=baseMapper.selectNoPassReportPage(page,dto,projectCodeList);
        return page;
    }

    @Override
    public String delete(String id) {
        LambdaUpdateWrapper<ZzwPierColumnReportEntity> update = Wrappers.lambdaUpdate(ZzwPierColumnReportEntity.class);
        update.eq(ZzwPierColumnReportEntity::getReportId,id).set(ZzwPierColumnReportEntity::getIsDelete,1);
        baseMapper.update(new ZzwPierColumnReportEntity(),update);
        return "删除成功";
    }

    /**
     * 递归查找所有projectCode
     * @param vo {@link ZzwPierProjectVo}
     * @return {@link List<String>}
     */
    private List<String> getZzwPierProjectVoList(ZzwPierProjectVo vo){
        ArrayList<String> list = new ArrayList<>();
        if (ObjectUtil.isNull(vo)) {
            return list;
        }
        list.add(vo.getId());
        if (ObjectUtil.isNotNull(vo.getSubsetList())) {
            for (ZzwPierProjectVo projectVo : vo.getSubsetList()) {
                list.addAll(getZzwPierProjectVoList(projectVo));
            }
        }
        return list;
    }
}
